<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogVisible"
    width="360px"
    top="5vh"
    @open="open"
  >
    <div class="user-display-center">
      <h-user-display v-model="value"></h-user-display>
    </div>
    <el-form :model="form" :rules="rules" labelPosition="top" ref="form" class="custom-form">
      <el-form-item label="新密码:" prop="password">
        <el-row :gutter="20">
          <el-col :span="24">
            <el-input type="password" v-model="form.password" placeholder="请输入新的密码"></el-input>
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item label="重复密码:" prop="rePassword">
        <el-row :gutter="20">
          <el-col :span="24">
            <el-input type="password" v-model="form.rePassword" placeholder="请重复输入密码"></el-input>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button type="primary" @click="changePwd"> 确定修改</el-button>
      <el-button @click="closed">关闭</el-button>
    </div>
  </el-dialog>

</template>


<script>
  import AdminService from 'src/service/admin'
  import Dialog from 'src/components/common/js/dialog-mix'
  import HUserDisplay from '../../common/display/HUserDisplay'

  export default {
    name: 'HAdminChangePasswordDialog',
    components: {HUserDisplay},
    mixins: [Dialog],
    data() {
      const checkPassword = (rule, value, callback) => {
        if (value == null) {
          callback(new Error('请再次输入密码!'))
        } else {
          if (this.form.password !== value) {
            callback(new Error('两次输入的密码不一致!'))
          } else {
            callback()
          }
        }
      }
      return {
        form: {
          password: '',
          rePassword: ''
        },
        rules: {
          password: [
            {required: true, message: '请输入新的密码', trigger: 'blur'},
            {min: 8, message: '最少8个字符', trigger: 'blur'}
          ],
          rePassword: [
            {required: true, message: '请再次输入密码', trigger: 'blur'},
            {validator: checkPassword, trigger: 'blur'}
          ],

        }
      }
    },
    watch: {
      value(current, old) {
        this.form = {
          password: '',
          rePassword: ''
        }
      }
    },
    props: {
      value: {
        type: Object,
        default: {}
      }
    },
    methods: {
      open: function () {
        this.form = {
          password: '',
          rePassword: ''
        }
      },
      changePwd: function () {
        const me = this
        this.$refs.form.validate((valid) => {
          if (valid) {
            AdminService.changepassword({
              oldPassword: me.form.password,
              password: me.form.rePassword,
              id: me.value.id
            }).then(ds => {
              if (ds != null) {
                me.$message.success('修改密码成功。')
                me.closed()
              } else {
                me.$message.error(ds.message)
              }
            })
          } else {
            return false
          }
        })

      }
    }
  }
</script>
